package com.yimixian.app.rest.api;

import com.yimixian.app.model.CancelOrder;
import com.yimixian.app.model.GoodImage;
import com.yimixian.app.model.GoodsItem;
import com.yimixian.app.model.Metadata;
import com.yimixian.app.model.Order;
import com.yimixian.app.model.SearchKeyResult;
import com.yimixian.app.model.SpeedOrder;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import retrofit.Callback;
import retrofit.client.Response;

/**
 * 订单列表
 * Created by zhilian-2 on 2015/9/21.
 */
public class OrdersListAPI extends  YmxBaseAPI<List<Order>>{

    public static final String ORDER_LIST = "/v5/orders/show";
    private String token = "";
    private int last_id;
    private int pagesize;
    public int back_last_id;

    public OrdersListAPI(int last_id, int pagesize, String token,  Callback<Response> callback) {
        super(callback);
        this.token = token;
        this.last_id = last_id;
        this.pagesize = pagesize;
    }

    @Override
    public String getRelativeURL() {
        return ORDER_LIST;
    }

    public void getOrdersList() {
        if (last_id == 0){
            mYmxRestService.getOrdersList(token, pagesize, mCallback);
        }else{
            mYmxRestService.getOrdersList(token, last_id, pagesize, mCallback);
        }
    }

    @Override
    public List<Order> parseJson(JSONObject jsonObject) {
        List<Order> list = new ArrayList<>();
        if(jsonObject != null){
            back_last_id = jsonObject.optInt("last_id");
            JSONArray jsonArray = jsonObject.optJSONArray("orders");
            if (jsonArray != null){
                for (int i = 0; i < jsonArray.length(); i++){
                    Order mOrder = new Order();
                    JSONObject object = jsonArray.optJSONObject(i);
                    mOrder.id = object.optInt("id");
                    mOrder.status = object.optInt("status");
                    List<Order.PaymentMethods> payMethodList = new ArrayList<>();
                    JSONArray payMethodArray = object.optJSONArray("payment_methods");
                    if (payMethodArray != null){
                        for (int j = 0; j < payMethodArray.length(); j++){
                            JSONObject payMethodObject = payMethodArray.optJSONObject(j);
                            if (payMethodObject != null){
                                Order.PaymentMethods paymentMethods = new Order.PaymentMethods();
                                paymentMethods.setId(payMethodObject.optString("id"));
                                paymentMethods.setName(payMethodObject.optString("name"));
                                payMethodList.add(paymentMethods);
                            }
                        }
                    }

                    mOrder.paymentMethods = payMethodList;

                    mOrder.paymentId = object.optString("payment_id");
                    mOrder.needsPayment = object.optBoolean("needs_payment");
                    mOrder.paymentDueAt = object.optString("payment_due_at");
                    mOrder.complainUrl = object.optString("complain_url");
                    mOrder.priceToPay = object.optString("price_to_pay");
                    mOrder.totalPrice = object.optString("total_price");

                    List<Metadata> metadataList = new ArrayList<>();
                    JSONArray priceComponents = object.optJSONArray("price_components");
                    if (priceComponents != null){
                        for (int j = 0; j < priceComponents.length(); j++){
                            JSONObject priceComponentsObject = priceComponents.optJSONObject(j);
                            if (priceComponentsObject != null){
                                Metadata metadata = new Metadata();
                                metadata.name = priceComponentsObject.optString("name");
                                metadata.url =  priceComponentsObject.optString("url");
                                metadata.value =  priceComponentsObject.optString("value");
                                metadataList.add(metadata);
                            }
                        }
                    }
                    mOrder.priceComponents = metadataList;

                    mOrder.successUrl = object.optString("success_url");

                    Order.DeliveryInfo deliveryInfo = new Order.DeliveryInfo();
                    JSONObject deliveryInfoObject = object.optJSONObject("delivery_info");
                    if (deliveryInfoObject != null){
                        deliveryInfo.index = deliveryInfoObject.optInt("index");
                        JSONArray statusArray = deliveryInfoObject.optJSONArray("statuses");
                        List<String> statuses = new ArrayList<>();
                        if (statusArray != null){
                            for (int j = 0; j < statusArray.length(); j++){
                                String str = statusArray.optString(j);
                                statuses.add(str);
                            }
                        }
                        deliveryInfo.statuses = statuses;
                    }
                    mOrder.deliveryInfo = deliveryInfo;

                    mOrder.createdAt = object.optString("created_at");
                    mOrder.totalCount = object.optInt("total_count");

                    JSONArray metadataArray = object.optJSONArray("metadata");
                    List<Metadata> metadata = new ArrayList<>();
                    if (metadataArray != null){
                        for (int j = 0; j < metadataArray.length(); j++){
                            JSONObject metadataObject = metadataArray.optJSONObject(j);
                            if (metadataObject != null){
                                Metadata met = new Metadata();
                                met.name = metadataObject.optString("name");
                                met.url =  metadataObject.optString("url");
                                met.value =  metadataObject.optString("value");
                                metadata.add(met);
                            }
                        }
                    }
                    mOrder.metadata = metadata;

                    JSONArray itemsArray = object.optJSONArray("items");
                    List<GoodsItem> goodsItems = new ArrayList<>();
                    if (itemsArray != null){
                        for (int j = 0; j < itemsArray.length(); j++){
                            JSONObject goodsItemObject = itemsArray.optJSONObject(j);
                            if (goodsItemObject != null){
                                GoodsItem met = new GoodsItem();
                                met.name = goodsItemObject.optString("name");
                                met.id = goodsItemObject.optInt("id");
                                met.canComment = goodsItemObject.optBoolean("can_comment");
                                met.tasteDesc = goodsItemObject.optString("taste_desc");
                                met.freshnessDesc = goodsItemObject.optString("freshness_desc");
                                met.count =  goodsItemObject.optString("count");
                                met.price =  goodsItemObject.optString("price");
                                goodsItems.add(met);
                            }
                        }
                    }
                    mOrder.items = goodsItems;

                    mOrder.statusUrl = object.optString("status_url");
                    mOrder.commentBtnName = object.optString("comment_btn_name");
                    mOrder.commentUrl = object.optString("comment_url");

                    //2.3.0
                    JSONArray goodImages = object.optJSONArray("good_img");
                    List<GoodImage> goodsImageList = new ArrayList<>();
                    if(goodImages != null){
                        for (int j = 0; j < goodImages.length(); j++){
                            JSONObject imgObj = goodImages.optJSONObject(j);
                            GoodImage img = new GoodImage();
                            img.id = imgObj.optString("id");
                            img.img = imgObj.optString("img");
                            img.img_ratio = (float) imgObj.optDouble("img_ratio");
                            goodsImageList.add(img);
                        }
                    }
                    mOrder.goodImages = goodsImageList;

                    JSONObject speedObject = object.optJSONObject("speed_order");
                    SpeedOrder speedOrder = new SpeedOrder();
                    if(speedObject != null){
                        speedOrder.message = speedObject.optString("message");
                        speedOrder.total_time = speedObject.optInt("total_time");
                        speedOrder.used_time = speedObject.optInt("used_time");
                        speedOrder.is_completed = speedObject.optBoolean("is_completed");
                    }
                    mOrder.speedOrder = speedOrder;

                    list.add(mOrder);
                }
            }
        }
        return list;
    }

}
